package com.gongpingjialoan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.amap.api.services.core.PoiItem;
import com.amap.api.services.geocoder.GeocodeResult;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeResult;
import com.amap.api.services.poisearch.PoiItemDetail;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.gongpingjialoan.R;

import java.util.List;

/**
 * Created by Administrator on 2016/5/28.
 */
public class AddressAdapter extends BaseAdapter implements PoiSearch.OnPoiSearchListener,
        GeocodeSearch.OnGeocodeSearchListener {

    private LayoutInflater mInflater;

    private List<PoiItem> mDatas;
    GeocodeSearch mGeoSearch;
    String provinceandcity;

    public AddressAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
        mGeoSearch = new GeocodeSearch(context);

    }


    public void setData(String provinceandcity, List<PoiItem> mDatas) {
        this.mDatas = mDatas;
        this.provinceandcity = provinceandcity;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (mDatas == null) {
            return 0;
        }
        return mDatas.size();
    }

    @Override
    public PoiItem getItem(int position) {
        // TODO Auto-generated method stub
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        PoiItem item = mDatas.get(position);

        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.listitem_suggestion, null);
            holder.nameText = (TextView) convertView.findViewById(R.id.tv_suggestion_name);
            holder.detailText = (TextView) convertView.findViewById(R.id.tv_suggestion_details);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.nameText.setText(item.getTitle());
//        mGeoSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
//            @Override
//            public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
//                holder.detailText.setText(regeocodeResult.getRegeocodeAddress().getFormatAddress());
//            }
//
//            @Override
//            public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
//
//            }
//        });
//        RegeocodeQuery query = new RegeocodeQuery(item.getLatLonPoint(), 200, GeocodeSearch.AMAP);
//        // 第一个参数表示一个Latlng，第二参数表示范围多少米，第三个参数表示是火系坐标系还是GPS原生坐标系
//        mGeoSearch.getFromLocationAsyn(query);// 设置同步逆地理编码请求
        holder.detailText.setText(provinceandcity + item.getSnippet());

        return convertView;
    }

    @Override
    public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {


    }

    @Override
    public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {

    }

    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {

    }

    @Override
    public void onPoiItemDetailSearched(PoiItemDetail poiItemDetail, int i) {
    }

    class ViewHolder {
        TextView nameText;

        TextView detailText;
    }
}

